:root {
  --bg: #f5f3ee;
  --surface: #ffffff;
  --text: #2b2a28;
  --muted: #7a766e;
  --line: #e3dfd6;
  --accent: #2f5d50;
  --accent-ink: #ffffff;
  --free: #e4f1e6;
  --free-ink: #2e6b3a;
  --busy: #c95f4a;
  --busy-ink: #ffffff;
  --swap: #f7e3dd;
  --danger: #b3261e;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1d1b; --surface: #262724; --text: #ecebe6; --muted: #a29e95; --line: #3a3b37;
    --accent: #6fb09b; --accent-ink: #10201b; --free: #26392b; --free-ink: #9fd3a9;
    --busy: #c2604c; --busy-ink: #fff; --swap: #e9c9c0; --danger: #ef6b5f;
  }
}

* { box-sizing: border-box; }
body { margin: 0; font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--accent); }
h1 { font-size: 1.6rem; margin: .2em 0 .6em; }
h2 { font-size: 1.2rem; margin: 1.4em 0 .6em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.container { max-width: 1100px; margin: 0 auto; padding: 20px 16px 60px; }

/* Barre du haut */
.topbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 24px; padding: 10px 16px; background: var(--accent); color: var(--accent-ink); }
.topbar a { color: var(--accent-ink); text-decoration: none; }
.topbar .brand { font-weight: 700; font-size: 1.1rem; }
.topbar nav { display: flex; flex-wrap: wrap; gap: 4px 16px; flex: 1; }
.topbar nav a { opacity: .85; } .topbar nav a:hover { opacity: 1; text-decoration: underline; }
.topbar .who { display: flex; gap: 12px; align-items: center; font-size: .9rem; }
.topbar .who form { margin: 0; }
.topbar button.link { color: var(--accent-ink); opacity: .8; }
body.admin .topbar { background: #3d3a35; color: #fff; }
body.admin .topbar a, body.admin .topbar button.link { color: #fff; }

/* Formulaires */
input, textarea, select { font: inherit; color: inherit; background: var(--surface); border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; width: 100%; }
input[type=checkbox] { width: auto; }
textarea { resize: vertical; }
label { display: flex; flex-direction: column; gap: 4px; font-size: .9rem; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }
button, .button { font: inherit; cursor: pointer; border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 6px; padding: 8px 14px; text-decoration: none; display: inline-block; }
button.primary, .button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.danger { color: var(--danger); }
button.link { background: none; border: none; padding: 0; color: var(--accent); text-decoration: underline; }
button.link.danger { color: var(--danger); }
.stack { display: flex; flex-direction: column; gap: 14px; }
.narrow { max-width: 380px; }
.row { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }
.grow { flex: 1; }
.card-form { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin: 16px 0; }
.card-form h2 { margin-top: 0; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; align-items: end; }
.grid-form h2, .grid-form .wide { grid-column: 1 / -1; }
.alert { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; }
.alert.error { background: #fbe4e1; color: #8a1c13; }
.alert.ok { background: #e2f2e5; color: #22582f; }

/* Connexion */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-box { width: 100%; max-width: 360px; background: var(--surface); padding: 28px; border-radius: 14px; box-shadow: 0 6px 30px rgba(0,0,0,.08); }
.login-box h1 { text-align: center; color: var(--accent); }

/* Tableaux */
table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius); overflow: hidden; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
td.actions { white-space: nowrap; display: flex; gap: 12px; }
td.actions form { margin: 0; }
tr.inactive td { opacity: .5; }
@media (max-width: 700px) { table { display: block; overflow-x: auto; } }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; text-decoration: none; color: var(--text); display: flex; flex-direction: column; }
.card b { font-size: 2rem; color: var(--accent); }

/* Calendrier */
.calendar { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin: 16px 0; }
.cal-toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.cal-toolbar h2 { margin: 0; font-size: 1.25rem; flex: 1; text-align: center; min-width: 200px; }
.btn-group { display: inline-flex; }
.btn-group button { border-radius: 0; margin-left: -1px; }
.btn-group button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.btn-group button:last-child { border-radius: 0 6px 6px 0; }
.btn-group button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn-group.nav button { font-weight: 600; }
.btn-group.nav .arrow { font-size: .8em; color: var(--accent); }
.cal-right { display: flex; gap: 8px; align-items: center; }
#cal-jump { width: auto; padding: 7px 8px; }
@media (max-width: 700px) {
  .cal-toolbar h2 { order: -1; flex-basis: 100%; }
  .btn-group.nav .lbl { display: none; }
  .btn-group.nav .arrow { font-size: 1em; }
  .cal-right { flex-basis: 100%; flex-wrap: wrap; }
  #cal-jump { flex: 1 1 100%; }
}

.cal-grid { display: grid; gap: 4px; }
.cal-grid.month { grid-template-columns: repeat(7, 1fr); }
.cal-grid.week { grid-template-columns: repeat(7, 1fr); }
.dow { text-align: center; font-size: .8rem; color: var(--muted); padding-bottom: 4px; }
.day { border-radius: 6px; padding: 6px; min-height: 64px; position: relative; overflow: hidden; }
.day.free { background: var(--free); color: var(--free-ink); }
.day.busy { background: var(--busy); color: var(--busy-ink); }
/* Arrivée = triangle bas-droit, départ = triangle haut-gauche ;
   changement d'occupant = les deux, séparés par une bande claire. */
.day.arr { background: linear-gradient(135deg, var(--free) 62%, var(--busy) 62%); color: var(--free-ink); }
.day.dep { background: linear-gradient(135deg, var(--busy) 38%, var(--free) 38%); color: var(--free-ink); }
.day.swap { background: linear-gradient(135deg, var(--busy) 42%, var(--swap) 42%, var(--swap) 58%, var(--busy) 58%); color: var(--busy-ink); }
.day.dep .num { color: var(--busy-ink); }
.day.out { opacity: .4; }
.day.today { outline: 2px solid var(--accent); outline-offset: 1px; }
.day .num { font-weight: 600; }
.day .mini { display: block; font-size: .72rem; line-height: 1.2; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.day.arr .mini, .day.swap .mini { color: var(--busy-ink); background: var(--busy); width: fit-content; max-width: 100%; margin: 14px 0 0 auto; padding: 1px 6px; border-radius: 4px; }
.week .day { min-height: 140px; }
.week .dhead { margin-bottom: 8px; line-height: 1.2; }
.week .dhead span { display: block; font-size: .8rem; text-transform: capitalize; opacity: .8; }
.week .dhead b { font-size: 1.4rem; }
.week .tag { font-size: .85rem; background: rgba(0,0,0,.15); color: inherit; padding: 4px 6px; border-radius: 4px; margin-top: 4px; }
.week .tag a { color: inherit; }
.week .tag em { font-size: .75rem; opacity: .85; }
.week .free-tag { background: transparent; padding-left: 0; }
.day.arr .tag, .day.dep .tag, .day.swap .tag { background: var(--busy); color: var(--busy-ink); }
.week .day.dep .dhead, .week .day.arr .dhead { color: var(--free-ink); }
@media (max-width: 700px) {
  .cal-grid.week { grid-template-columns: 1fr; }
  .week .day { min-height: auto; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
  .week .dhead { margin: 0; min-width: 120px; }
  .day { min-height: 46px; padding: 4px; }
  .day .mini { display: none; }
}

.legend { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 12px; font-size: .85rem; color: var(--muted); }
.legend .sw { display: inline-block; width: 14px; height: 14px; border-radius: 3px; vertical-align: -2px; margin-right: 6px; }
.sw.free { background: var(--free); border: 1px solid var(--line); }
.sw.busy { background: var(--busy); }
.sw.arr { background: linear-gradient(135deg, var(--free) 55%, var(--busy) 55%); }
.sw.dep { background: linear-gradient(135deg, var(--busy) 45%, var(--free) 45%); }
.sw.swap { background: linear-gradient(135deg, var(--busy) 40%, var(--swap) 40%, var(--swap) 60%, var(--busy) 60%); }
.cal-list h3 { font-size: 1rem; margin: 16px 0 6px; }
.cal-list ul { margin: 0; padding-left: 20px; }

/* Mode d'emploi */
.guide { display: grid; grid-template-columns: 220px 1fr; gap: 24px; }
.guide-nav h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-top: 0; }
.guide-nav ul { list-style: none; padding: 0; margin: 0; }
.guide-nav a { display: block; padding: 6px 10px; border-radius: 6px; text-decoration: none; color: var(--text); }
.guide-nav a.active { background: var(--accent); color: var(--accent-ink); }
.guide-body { background: var(--surface); border-radius: var(--radius); padding: 20px 28px; }
@media (max-width: 700px) {
  .guide { grid-template-columns: 1fr; }
  .guide-nav ul { display: flex; flex-wrap: wrap; gap: 6px; }
  .guide-nav a { border: 1px solid var(--line); }
  .guide-body { padding: 16px; }
}
.prose img { max-width: 100%; height: auto; border-radius: 8px; }
.prose img.inline { height: 1.4em; width: auto; vertical-align: middle; border-radius: 3px; }
.prose figure { margin: 16px 0; max-width: 520px; }
.prose figure.small { max-width: 260px; }
.prose figure img { display: block; }
.prose figcaption { font-size: .85rem; color: var(--muted); margin-top: 6px; }
.prose .gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; margin: 16px 0; }
.prose .gallery figure { margin: 0; }
.prose .gallery img { width: 100%; cursor: zoom-in; }
.prose .gallery.photos img { aspect-ratio: 4 / 3; object-fit: cover; }
.prose blockquote { margin: 16px 0; padding: 10px 16px; border-radius: 8px; background: var(--free); color: var(--text); }
.prose blockquote p { margin: 4px 0; }
.prose code { background: var(--bg); padding: 1px 6px; border-radius: 4px; font-size: .95em; }
.prose th, .prose td { border: 1px solid var(--line); }
.prose th { text-transform: none; letter-spacing: 0; font-size: .9rem; }
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 50; cursor: zoom-out; padding: 16px; }
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 6px; }
.prose h2 { border-bottom: 1px solid var(--line); padding-bottom: 4px; }
.prose table { border: 1px solid var(--line); }

/* Éditeur */
.editor { display: flex; flex-direction: column; gap: 12px; }
.editor-panes { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.editor-panes textarea { font-family: ui-monospace, Consolas, monospace; font-size: .9rem; min-height: 420px; }
.editor .label { font-size: .9rem; color: var(--muted); }
.preview { background: var(--surface); border: 1px solid var(--line); border-radius: 6px; padding: 8px 16px; min-height: 420px; max-height: 70vh; overflow: auto; margin-top: 4px; }
@media (max-width: 800px) { .editor-panes { grid-template-columns: 1fr; } }
.help { margin-top: 16px; } .help pre { background: var(--surface); padding: 12px; border-radius: 6px; }
.thumbs { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.thumb { padding: 2px; width: 96px; height: 72px; overflow: hidden; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb.pdf { width: auto; font-size: .8rem; }
.media-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.media-grid figure { margin: 0; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.media-grid img { width: 100%; height: 140px; object-fit: cover; display: block; }
.media-grid .pdf { display: grid; place-items: center; height: 140px; font-size: 3rem; text-decoration: none; }
.media-grid figcaption { padding: 8px; font-size: .75rem; display: flex; flex-direction: column; gap: 6px; word-break: break-all; }
.media-grid form { margin: 0; }
