:root {
  color-scheme: light dark;
  --bg: #f4f2ee;
  --card: #ffffff;
  --text: #1d1d1f;
  --muted: #6e6e73;
  --line: #e6e2dc;
  --accent: #2f7d6d;
  --accent-text: #ffffff;
  --accent-weak: #e2efeb;
  --danger: #c0392b;
  --late: #c0392b;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 1px 1px rgb(0 0 0 / 0.03);
  --radius: 14px;
  --tabbar-h: 58px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121212;
    --card: #1e1e20;
    --text: #f2f2f7;
    --muted: #9a9aa0;
    --line: #2e2e31;
    --accent: #5fbfa8;
    --accent-text: #08201b;
    --accent-weak: #1d3530;
    --danger: #ff6b5e;
    --late: #ff8a7e;
    --danger-bg: #b3261e;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font-size: 16px; line-height: 1.35; min-height: 100dvh; }

h1, h2, h3 { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
/* 16 px minimum : sinon Safari iOS zoome au focus. */
input, select, textarea { font-size: 16px; }

.icon {
  width: 22px; height: 22px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); margin: 0; }

/* ------------------------------------------------------------ structure */
.app { padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-t) + 10px) 18px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
}
.topbar h1 { font-size: 30px; font-weight: 750; letter-spacing: -0.02em; }
.avatar-btn { border: 0; background: none; padding: 0; cursor: pointer; }
.avatar-btn .avatar { width: 36px; height: 36px; font-size: 16px; }

.view { max-width: 640px; margin: 0 auto; padding: 4px 14px 0; display: flex; flex-direction: column; gap: 14px; }
.stack { display: flex; flex-direction: column; gap: 14px; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--card) 90%, transparent);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  border-top: 0.5px solid var(--line);
}
.tabbar button {
  border: 0; background: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--muted); font-size: 11px; font-weight: 600;
}
.tabbar button.active { color: var(--accent); }

.offline {
  position: fixed; top: var(--safe-t); left: 0; right: 0; z-index: 50;
  background: #8a6d1f; color: #fff; text-align: center; font-size: 13px; padding: 6px 10px;
}

/* ------------------------------------------------------------ éléments */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px; border-radius: 12px; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); font-weight: 600; text-decoration: none;
}
.btn .icon { width: 20px; height: 20px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.6; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }

.btn-icon {
  width: 40px; height: 40px; flex: none; border-radius: 50%; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--line); background: var(--card);
}
.btn-icon.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-icon.ghost { border-color: transparent; background: transparent; color: var(--muted); }

.link { border: 0; background: none; padding: 4px 0; color: var(--accent); font-weight: 600; cursor: pointer; text-decoration: none; }
.link.small { font-size: 13px; }

.avatar {
  --c: var(--accent);
  width: 28px; height: 28px; flex: none; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--c); color: #fff; font-weight: 700; font-size: 13px;
}
.avatar.small { width: 22px; height: 22px; font-size: 11px; }
.both { display: inline-flex; }
.both .avatar + .avatar { margin-left: -6px; box-shadow: 0 0 0 2px var(--card); }

.empty { text-align: center; color: var(--muted); padding: 28px 12px; margin: 0; }

.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; }

.segmented {
  display: flex; padding: 3px; gap: 3px; border-radius: 11px;
  background: color-mix(in srgb, var(--muted) 16%, transparent);
  overflow-x: auto;
}
.segmented button {
  flex: 1; border: 0; background: none; border-radius: 9px; padding: 7px 10px; cursor: pointer;
  font-size: 14px; font-weight: 600; color: var(--muted); white-space: nowrap;
}
.segmented button.active { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

/* ------------------------------------------------------------ formulaires */
input[type=text], input[type=password], input[type=url], input[type=date], input[type=time], input:not([type]), select, textarea {
  width: 100%; min-height: 44px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--card);
  -webkit-appearance: none; appearance: none;
}
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%;
  background-size: 5px 5px; background-repeat: no-repeat; padding-right: 32px;
}
textarea { resize: vertical; line-height: 1.4; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field-label { font-size: 13px; font-weight: 600; color: var(--muted); }
.row-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.switch input {
  -webkit-appearance: none; appearance: none; margin: 0; cursor: pointer;
  width: 46px; height: 28px; border-radius: 14px; background: var(--line); position: relative; transition: background 0.15s;
}
.switch input::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 24px; height: 24px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgb(0 0 0 / 0.3); transition: transform 0.15s;
}
.switch input:checked { background: var(--accent); }
.switch input:checked::after { transform: translateX(18px); }

.add-bar { display: flex; gap: 8px; padding: 8px; align-items: center; }
.add-bar input { border: 0; background: transparent; min-width: 0; flex: 1; }
.add-bar input.qty { flex: 0 0 72px; border-left: 1px solid var(--line); border-radius: 0; }
.add-bar select.compact { flex: 0 0 auto; width: auto; max-width: 130px; border: 0; background-color: transparent; }
.add-bar input:focus, .add-bar select:focus { outline: none; }

/* ------------------------------------------------------------ listes */
.list { list-style: none; margin: 0; padding: 0; overflow: hidden; }
.row { display: flex; align-items: center; gap: 10px; padding: 0 12px; min-height: 52px; }
.row + .row { border-top: 0.5px solid var(--line); }
.row-main {
  flex: 1; min-width: 0; align-self: stretch; text-align: left; cursor: pointer;
  border: 0; background: none; padding: 10px 0;
  display: flex; flex-direction: column; justify-content: center; gap: 2px;
}
.row-title { overflow-wrap: anywhere; }
.row-meta { font-size: 13px; color: var(--muted); display: inline-flex; align-items: center; gap: 4px; }
.row-meta .icon { width: 13px; height: 13px; }
.row-meta.late { color: var(--late); font-weight: 600; }

.check {
  width: 26px; height: 26px; flex: none; border-radius: 50%; cursor: pointer; padding: 0;
  border: 2px solid color-mix(in srgb, var(--muted) 60%, transparent); background: none;
  display: inline-flex; align-items: center; justify-content: center; color: transparent;
}
.check .icon { width: 16px; height: 16px; stroke-width: 3; }
.row.checked .check { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.row.checked .row-title { color: var(--muted); text-decoration: line-through; }

.group-title { font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 0 4px 6px; }
.group-head { display: flex; align-items: baseline; justify-content: space-between; margin-right: 4px; }
.group-head .group-title { margin-bottom: 6px; }
.group-actions { display: flex; gap: 14px; }

/* ------------------------------------------------------------ repas */
.week-nav { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.week-label { display: flex; flex-direction: column; align-items: center; text-align: center; }
.day { padding: 10px 12px 6px; }
.day.today { box-shadow: 0 0 0 2px var(--accent); }
.day.past { opacity: 0.6; }
.day-head { font-weight: 700; margin-bottom: 4px; }
.day.today .day-head { color: var(--accent); }
.slot {
  width: 100%; display: flex; align-items: center; gap: 10px; text-align: left; cursor: pointer;
  border: 0; background: none; padding: 9px 0; min-height: 44px;
}
.slot + .slot { border-top: 0.5px solid var(--line); }
.slot-label { flex: 0 0 40px; font-size: 13px; font-weight: 600; color: var(--muted); }
.slot-text { flex: 1; }
.slot-empty { flex: 1; color: color-mix(in srgb, var(--muted) 70%, transparent); }
.slot-badge { color: var(--accent); display: inline-flex; }
.slot-badge .icon { width: 17px; height: 17px; }

/* ------------------------------------------------------------ agenda */
.row-main.event { flex-direction: row; align-items: center; justify-content: flex-start; gap: 12px; }
.event-time { flex: 0 0 52px; font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; white-space: pre-line; line-height: 1.2; }
.event-body { display: flex; flex-direction: column; gap: 2px; min-width: 0; }

/* ------------------------------------------------------------ feuilles (modales) */
.backdrop {
  position: fixed; inset: 0; z-index: 100; background: rgb(0 0 0 / 0.35);
  display: flex; align-items: flex-end; justify-content: center;
  animation: fade-in 0.18s ease-out;
}
.backdrop.closing { animation: fade-out 0.2s ease-in forwards; }
.sheet {
  width: 100%; max-width: 560px; max-height: 92dvh; display: flex; flex-direction: column;
  background: var(--bg); border-radius: 18px 18px 0 0;
  animation: slide-up 0.22s cubic-bezier(0.2, 0.9, 0.3, 1);
}
.backdrop.closing .sheet { animation: slide-down 0.2s ease-in forwards; }
@media (min-width: 700px) {
  .backdrop { align-items: center; }
  .sheet { border-radius: 18px; }
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 14px 10px 6px 18px; }
.sheet-head h2 { font-size: 19px; }
.sheet-body { overflow-y: auto; padding: 8px 18px 16px; display: flex; flex-direction: column; gap: 14px; overscroll-behavior: contain; }
.sheet-actions {
  display: flex; gap: 10px; padding: 10px 18px calc(12px + var(--safe-b));
  border-top: 0.5px solid var(--line);
}
.sheet-actions .btn { flex: 1; }
.sheet-actions .btn.danger { flex: 0 1 auto; }
.me-line { display: flex; align-items: center; gap: 12px; }
.me-line .avatar { width: 44px; height: 44px; font-size: 19px; }

@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }
@keyframes slide-up { from { transform: translateY(100%); } }
@keyframes slide-down { to { transform: translateY(100%); } }
@media (prefers-reduced-motion: reduce) {
  .backdrop, .sheet, .backdrop.closing, .backdrop.closing .sheet { animation-duration: 0.01s; }
}

/* ------------------------------------------------------------ toasts */
.toasts {
  position: fixed; left: 0; right: 0; z-index: 200; pointer-events: none;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 12px);
  display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 0 16px;
}
.toast {
  max-width: 480px; padding: 10px 16px; border-radius: 12px; font-size: 14px;
  background: #222; color: #fff; box-shadow: 0 4px 16px rgb(0 0 0 / 0.2);
  transition: opacity 0.3s, transform 0.3s;
}
.toast.error { background: var(--danger-bg, var(--danger)); color: var(--danger-on, #fff); }
.toast.out { opacity: 0; transform: translateY(8px); }

/* ------------------------------------------------------------ connexion */
.login { min-height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 24px 18px; }
.login-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; gap: 14px; text-align: center; }
.login-card .field { text-align: left; }
.login-logo { align-self: center; border-radius: 18px; }
.login-card h1 { font-size: 30px; }
.login-card p { margin: 0; }
