/* Tailles d'écran : téléphone <= 640px, tablette / petit écran <= 960px, ordinateur au-delà.
   Garder PAGE_MAX et PHONE_MAX (app.js) en accord avec .main et les media queries ci-dessous. */

:root {
  --bg: #f4f5f9;
  --surface: #ffffff;
  --surface-2: #eef0f6;
  --border: #e2e5ee;
  --text: #1b1f2e;
  --muted: #667085;
  --primary: #4f46e5;
  --on-primary: #ffffff;
  --danger: #dc2626;
  --warn-bg: #fff7e6;
  --warn-border: #f5c26b;
  --free: #15803d;
  --free-bg: rgba(34, 197, 94, 0.15);
  --free-line: rgba(22, 163, 74, 0.65);
  --now: #ef4444;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 2px 8px rgba(16, 24, 40, .05);
  color-scheme: light;
}

/* data-theme est posé par theme.js : choix de la personne, sinon thème du système. */
:root[data-theme="dark"] {
  --bg: #0e1016;
  --surface: #161922;
  --surface-2: #1f2330;
  --border: #2a2f3e;
  --text: #e8eaf1;
  --muted: #98a2b3;
  --primary: #8b93ff;
  --on-primary: #0e1016;
  --danger: #f87171;
  --warn-bg: #2a2212;
  --warn-border: #7a5a1c;
  --free: #4ade80;
  --free-bg: rgba(74, 222, 128, 0.13);
  --free-line: rgba(74, 222, 128, 0.55);
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button, input, select { font: inherit; color: inherit; }
button { touch-action: manipulation; }
h1, h2 { margin: 0; line-height: 1.25; }
h1 { font-size: 24px; }
h2 { font-size: 17px; }
p { margin: 0; }
a { color: var(--primary); }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }

/* ---------- Boutons et champs ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary); filter: brightness(1.08); }
.btn-danger { color: var(--danger); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-sm { padding: 5px 11px; font-size: 13px; }
.btn-block { width: 100%; }
.btn-icon { flex: none; width: 36px; height: 36px; padding: 0; font-size: 20px; line-height: 1; }

input[type="text"], input[type="password"], input[type="url"], input[type="search"], input[type="time"], select {
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
}
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field-label { font-size: 14px; font-weight: 600; }
.field-hint { font-size: 12px; color: var(--muted); }
.stack { display: flex; flex-direction: column; gap: 14px; }
.form-spaced { margin-top: 14px; }

.segmented {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
}
.segmented button {
  padding: 6px 12px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  display: inline-block;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Connexion ---------- */

.auth-page { min-height: 100vh; min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.auth-card { width: min(410px, 100%); display: flex; flex-direction: column; gap: 18px; padding: 28px; }
.auth-brand { display: flex; align-items: center; justify-content: center; gap: 10px; }
.auth-card .segmented { align-self: stretch; }
.auth-card .segmented button { flex: 1; }

/* ---------- Structure ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-size: 17px; font-weight: 700; white-space: nowrap; }
.tabs { display: flex; gap: 4px; margin: 0 auto; }
.tab {
  display: inline-flex;
  align-items: center;
  padding: 8px 14px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
}
.tab:hover { background: var(--surface-2); color: var(--text); }
.tab.active { background: color-mix(in srgb, var(--primary) 13%, transparent); color: var(--primary); }
.badge {
  margin-left: 6px;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  line-height: 1.4;
}
.who { display: flex; align-items: center; gap: 8px; white-space: nowrap; }

/* Barre d'onglets du bas : seulement sur téléphone. */
.bottom-nav { display: none; }
.bottom-tab {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 6px 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
}
.bottom-tab.active { color: var(--primary); }
.bottom-tab .badge { position: absolute; top: 0; left: calc(50% + 6px); margin: 0; padding: 0 6px; font-size: 11px; }

.main { max-width: 1440px; margin: 0 auto; padding: 20px; }
.view { display: flex; flex-direction: column; gap: 16px; }
.view.narrow { width: 100%; max-width: 760px; margin: 0 auto; }

.card {
  min-width: 0;
  padding: 16px 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.card > h2 + * { margin-top: 10px; }
.card.empty { padding: 40px 16px; text-align: center; color: var(--muted); }

.banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-left: 4px solid var(--primary);
}
.notice {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 14px;
  border: 1px solid var(--warn-border);
  border-radius: var(--radius);
  background: var(--warn-bg);
  font-size: 14px;
}

.avatar {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--c, #94a3b8);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
}
.avatar-sm { width: 22px; height: 22px; font-size: 11px; }
.avatar-group { background: color-mix(in srgb, var(--primary) 15%, var(--surface)); color: var(--primary); }
.avatar-group svg { width: 18px; height: 18px; }

.tag {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

/* ---------- Barre d'outils de comparaison ---------- */

.toolbar { display: flex; flex-direction: column; gap: 14px; }
.toolbar-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; }
.week-nav { display: flex; align-items: center; gap: 8px; min-width: 0; }
.week-label { min-width: 230px; text-align: center; }
.loading-inline { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.options { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.inline { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 14px; }
.hint { color: var(--muted); font-size: 13px; }

.picker { display: flex; align-items: center; gap: 12px; min-width: 0; }
.picker-label { flex: none; width: 76px; color: var(--muted); font-size: 13px; font-weight: 600; }

.chips { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; min-width: 0; }
.chip {
  display: inline-flex;
  flex: none;
  align-items: center;
  gap: 8px;
  padding: 5px 12px 5px 5px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
}
.chip[aria-pressed="true"] { border-color: var(--c); background: color-mix(in srgb, var(--c) 12%, var(--surface)); }
.chip[aria-pressed="false"] { color: var(--muted); }
.chip .avatar { width: 24px; height: 24px; font-size: 12px; }
.chip[aria-pressed="false"] .avatar { background: transparent; border: 2px solid var(--c); color: var(--c); }
.chip-warn { font-size: 11px; color: var(--muted); font-weight: 400; }
.chip-group { --c: var(--primary); padding-left: 10px; }
.chip-group svg { width: 18px; height: 18px; }
.chip-group[aria-pressed="true"] { color: var(--primary); }
.chip-count { padding: 0 7px; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: 12px; }
.chip-add { padding: 5px 12px; border-style: dashed; color: var(--muted); }
.chip-add:hover { border-color: var(--c); color: var(--text); }

/* ---------- Résumé des trous communs ---------- */

.results { display: flex; flex-direction: column; gap: 16px; transition: opacity .2s; }
.results.is-loading { opacity: .55; }
.summary-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 8px; }
.summary-total { color: var(--free); font-weight: 600; font-size: 14px; }
.summary-days { display: flex; flex-wrap: wrap; gap: 12px 28px; margin-top: 12px; }
.summary-day { display: flex; flex-direction: column; gap: 6px; }
.summary-day-name { font-size: 13px; font-weight: 600; color: var(--muted); }
.slot-list { display: flex; flex-wrap: wrap; gap: 6px; }
.slot-chip {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--free-bg);
  color: var(--free);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}
.slot-chip small { font-weight: 500; opacity: .8; }

/* ---------- Grille ---------- */

.grid-card { padding: 8px 10px 12px; }
.grid-scroll { overflow-x: auto; overscroll-behavior-x: contain; }
.grid {
  display: grid;
  grid-template-columns: var(--time-col) repeat(var(--days), minmax(var(--day-min), 1fr));
  min-width: min-content;
}
/* La colonne des heures reste visible quand la grille défile sur le côté. */
.grid-corner, .time-col { position: sticky; left: 0; z-index: 4; background: var(--surface); }
.grid-corner { border-bottom: 1px solid var(--border); }
.day-head { min-width: 0; padding: 8px 0 10px; border-bottom: 1px solid var(--border); text-align: center; }
.day-name { font-weight: 600; }
.day-date { color: var(--muted); font-size: 13px; }
.day-head.today .day-name, .day-head.today .day-date { color: var(--primary); }
.lane-heads { display: flex; gap: 3px; margin-top: 6px; padding: 0 3px; }
.lane-head { display: flex; flex: 1; flex-direction: column; align-items: center; gap: 3px; min-width: 0; }
.lane-name {
  max-width: 100%;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.time-col { position: sticky; }
.time-label { position: absolute; right: 8px; transform: translateY(-50%); color: var(--muted); font-size: 12px; }

.day-col {
  position: relative;
  display: flex;
  gap: 3px;
  padding: 0 3px;
  border-left: 1px solid var(--border);
  background-image: repeating-linear-gradient(to bottom, var(--border) 0 1px, transparent 1px var(--hour));
  background-position: 0 var(--pad);
}
.day-col.today { background-color: color-mix(in srgb, var(--primary) 5%, transparent); }
.lane { position: relative; flex: 1; min-width: 0; }

.event {
  position: absolute;
  z-index: 2;
  overflow: hidden;
  padding: 3px 5px;
  border-left: 3px solid var(--c);
  border-radius: 6px;
  background: color-mix(in srgb, var(--c) 16%, var(--surface));
  font-size: 12px;
  line-height: 1.25;
}
.ev-title, .ev-meta { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.ev-title { font-weight: 600; }
.ev-meta { color: var(--muted); font-size: 11px; }
.grid-day .event { padding: 4px 7px; font-size: 13px; }
.grid-day .ev-meta { font-size: 12px; }
.event.cancelled {
  opacity: .6;
  border-left-style: dashed;
  background: repeating-linear-gradient(135deg, color-mix(in srgb, var(--c) 12%, var(--surface)) 0 6px, var(--surface) 6px 12px);
}
.event.cancelled .ev-title { text-decoration: line-through; }

.free {
  position: absolute;
  left: 0;
  right: 0;
  z-index: 1;
  border-top: 2px dashed var(--free-line);
  border-bottom: 2px dashed var(--free-line);
  background: var(--free-bg);
  pointer-events: none;
}
.free-label {
  position: absolute;
  top: 3px;
  left: 4px;
  max-width: calc(100% - 8px);
  overflow: hidden;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--surface);
  color: var(--free);
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.now-line { position: absolute; left: 0; right: 0; z-index: 3; height: 2px; background: var(--now); }
.now-line::before {
  content: "";
  position: absolute;
  left: -4px;
  top: -3px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--now);
}

/* Onglets des jours, quand la semaine ne tient pas à l'écran. */
.day-tabs { display: flex; gap: 6px; margin: 2px 0 8px; }
.day-tab {
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  min-width: 0;
  padding: 6px 2px 7px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
}
.day-tab:hover { background: var(--surface-2); }
.day-tab-name { font-size: 12px; font-weight: 600; }
.day-tab-date { color: var(--text); font-size: 17px; font-weight: 700; line-height: 1.2; }
.day-tab.today .day-tab-date { color: var(--primary); }
.day-tab.active { border-color: var(--primary); background: color-mix(in srgb, var(--primary) 12%, var(--surface)); }
.day-tab.active .day-tab-name { color: var(--primary); }
.day-tab-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--free); }
.day-tab-dot.empty { background: transparent; }

/* ---------- Amis et groupes ---------- */

.list { margin: 0; padding: 0; list-style: none; }
.person-row { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-top: 1px solid var(--border); }
.person-row:first-child { border-top: 0; }
.person-info { flex: 1; min-width: 0; }
.person-name { font-weight: 600; overflow-wrap: anywhere; }
.person-name .tag { margin-left: 6px; vertical-align: 1px; }
.person-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; }
.search-results { margin-top: 8px; }
.card input[type="search"] { width: 100%; margin-top: 12px; }

.group-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; }
.group-title { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.group-title h2 { overflow-wrap: anywhere; }
.group-card .list { margin-top: 8px; }
.group-invite {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  margin-top: 6px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

/* ---------- Profil ---------- */

.row-form { display: flex; gap: 8px; margin-top: 12px; }
.row-form input { flex: 1; }
.status { margin-top: 10px; font-size: 14px; }
.status.ok { color: var(--free); font-weight: 500; }
.status.error { color: var(--danger); }
.steps { margin: 12px 0 0; padding-left: 20px; color: var(--muted); font-size: 14px; }
.steps li + li { margin-top: 4px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.privacy { margin-top: 12px; font-size: 13px; color: var(--muted); }
details { margin-top: 14px; }
details > summary { cursor: pointer; color: var(--primary); font-size: 14px; font-weight: 500; }
details > :not(summary) { margin-top: 10px; }
.file-input { display: none; }
.danger-zone > summary { color: var(--danger); }

/* ---------- Notifications ---------- */

#toasts {
  position: fixed;
  bottom: 20px;
  left: 50%;
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: max-content;
  max-width: calc(100vw - 32px);
  transform: translateX(-50%);
  pointer-events: none;
}
.toast {
  padding: 10px 16px;
  border-radius: 10px;
  background: #1b1f2e;
  color: #fff;
  font-size: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
  transition: opacity .4s, transform .4s;
}
.toast-success { background: #15803d; }
.toast-error { background: #b91c1c; }
.toast.leaving { opacity: 0; transform: translateY(8px); }

/* ---------- Écrans moyens (tablette, petite fenêtre) ---------- */

@media (max-width: 960px) {
  .who-name { display: none; }
  .tab { padding: 8px 10px; }
}

@media (max-width: 820px) {
  .brand-name { display: none; }
}

/* ---------- Téléphone ---------- */

@media (max-width: 640px) {
  .topbar { gap: 8px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); }
  .topbar .tabs { display: none; }
  .brand-name { display: inline; }
  .who { margin-left: auto; }

  .bottom-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--border);
    background: var(--surface);
  }

  .main { padding: 12px 12px calc(84px + env(safe-area-inset-bottom)); }
  .view { gap: 12px; }
  .card { padding: 14px; border-radius: 12px; }
  h2 { font-size: 16px; }

  /* Évite le zoom automatique d'iOS sur les champs de moins de 16px. */
  input[type="text"], input[type="password"], input[type="url"], input[type="search"], input[type="time"], select {
    font-size: 16px;
  }

  .toolbar { gap: 12px; }
  .toolbar-row { flex-direction: column; align-items: stretch; gap: 10px; }
  .week-nav { justify-content: space-between; }
  .week-label { flex: 1; min-width: 0; font-size: 14px; }
  .options { justify-content: space-between; }
  .options .segmented, .theme-choice { width: 100%; }
  .options .segmented button, .theme-choice button { flex: 1; }
  .picker { flex-direction: column; align-items: stretch; gap: 6px; }
  .picker-label { width: auto; }
  /* Sur téléphone, les puces défilent sur le côté au lieu de prendre plusieurs lignes. */
  .chips-scroll { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
  .chips-scroll::-webkit-scrollbar { display: none; }

  .banner, .row-form { flex-direction: column; align-items: stretch; }
  .summary-days { gap: 10px 18px; }
  .grid-card { padding: 8px; }
  .auth-card { padding: 22px 18px; }
  #toasts { bottom: calc(84px + env(safe-area-inset-bottom)); }
}

@media (max-width: 480px) {
  .person-row { flex-wrap: wrap; }
  .person-actions { width: 100%; justify-content: flex-start; padding-left: 44px; }
  .person-actions:empty { display: none; }
  .group-head > .btn { width: 100%; }
}

/* ---------- Écrans tactiles : cibles plus grandes ---------- */

@media (pointer: coarse) {
  .btn { min-height: 40px; }
  .btn-sm { min-height: 36px; }
  .chip { min-height: 40px; }
  .segmented button { min-height: 36px; }
}

@media (prefers-reduced-motion: reduce) {
  .results, .toast { transition: none; }
}
