:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --line-strong: #cbd5e1;
  --accent: #2563eb;
  --today-bg: #eff6ff;
  --now: #ef4444;
  --bad: #b45309;
  --shadow: 0 1px 2px rgb(15 23 42 / .06), 0 4px 16px rgb(15 23 42 / .06);
  --radius: 14px;
  --ev-mix: 18%;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #0b1120;
    --surface: #111827;
    --surface-2: #0f172a;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --line: #1f2937;
    --line-strong: #334155;
    --accent: #3b82f6;
    --today-bg: #121d38;
    --bad: #fbbf24;
    --shadow: none;
    --ev-mix: 32%;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #0f172a;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --line: #1f2937;
  --line-strong: #334155;
  --accent: #3b82f6;
  --today-bg: #121d38;
  --bad: #fbbf24;
  --shadow: none;
  --ev-mix: 32%;
  color-scheme: dark;
}

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

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.3 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

/* ---------- Barre du haut ---------- */

.topbar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 24px;
  padding: max(12px, env(safe-area-inset-top)) 20px 12px;
}

.clock-time {
  font-size: 3rem;
  font-weight: 700;
  letter-spacing: -.02em;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.clock-date { color: var(--muted); font-size: 1.05rem; margin-top: 4px; }

.nav { display: flex; align-items: center; justify-content: center; gap: 16px; flex-wrap: wrap; }
.period { font-size: 1.5rem; font-weight: 650; min-width: 12ch; }

.segmented {
  display: inline-flex;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 4px;
  box-shadow: var(--shadow);
}
.segmented button {
  padding: 10px 22px;
  border-radius: 999px;
  font-weight: 600;
  color: var(--muted);
}
.segmented button[aria-selected="true"] { background: var(--accent); color: #fff; }

.nav-buttons { display: inline-flex; align-items: center; gap: 6px; }
.icon-btn {
  width: 48px; height: 48px;
  border-radius: 50%;
  font-size: 1.8rem;
  line-height: 1;
  display: inline-grid; place-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
}
.icon-btn.small { width: 40px; height: 40px; font-size: 1.15rem; }
.today-btn {
  height: 48px;
  padding: 0 20px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-weight: 600;
}
.icon-btn:active, .today-btn:active, .segmented button:active { transform: scale(.96); }

.side { display: flex; flex-direction: column; align-items: flex-end; gap: 8px; }
.people { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.person {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 16px 8px 12px;
  border-radius: 999px;
  border: 2px solid var(--c);
  background: color-mix(in srgb, var(--c) var(--ev-mix), var(--surface));
  font-weight: 600;
}
.person .dot { width: 14px; height: 14px; border-radius: 50%; background: var(--c); }
.person[aria-pressed="false"] { opacity: .45; background: var(--surface); }
.person[aria-pressed="false"] .person-name { text-decoration: line-through; }
.person .warn {
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--bad); color: #fff; font-size: .8rem; font-weight: 800;
  display: inline-grid; place-items: center;
}

.tools { display: flex; align-items: center; gap: 6px; }
.status { color: var(--muted); font-size: .85rem; margin-right: 6px; max-width: 40ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status.bad { color: var(--bad); }
#refreshBtn.spinning { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Zone calendrier ---------- */

.calendar {
  flex: 1;
  min-height: 0;
  margin: 0 20px max(16px, env(safe-area-inset-bottom));
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  touch-action: pan-y;
}

/* ---------- Semaine ---------- */

.week { display: flex; flex-direction: column; height: 100%; }
.wk-row { display: grid; grid-template-columns: 56px repeat(7, minmax(0, 1fr)); }
.gutter { color: var(--muted); font-size: .8rem; }

.wk-head { border-bottom: 1px solid var(--line); }
.wk-day-head {
  display: flex; align-items: baseline; justify-content: center; gap: 8px;
  padding: 10px 4px;
  border-left: 1px solid var(--line);
}
.wk-day-head .wd { color: var(--muted); font-weight: 600; }
.wk-day-head .dn { font-size: 1.5rem; font-weight: 700; }
.wk-day-head.today .dn {
  background: var(--accent); color: #fff;
  min-width: 2.1em; height: 2.1em; border-radius: 50%;
  display: inline-grid; place-items: center; font-size: 1.2rem;
}

.wk-allday { border-bottom: 1px solid var(--line-strong); max-height: 22%; overflow: hidden; }
.wk-allday .gutter { padding: 8px 6px; text-align: right; }
.wk-allday-cell {
  border-left: 1px solid var(--line);
  padding: 4px;
  display: flex; flex-direction: column; gap: 3px;
  min-width: 0;
}
.wk-allday-cell.today, .wk-col.today { background-color: var(--today-bg); }
.chip {
  display: block; width: 100%;
  text-align: left;
  padding: 4px 8px;
  border-radius: 6px;
  background: var(--c);
  color: #fff;
  font-weight: 600; font-size: .9rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.wk-body { flex: 1; min-height: 0; }
.wk-hours { position: relative; }
.hour-label { position: absolute; right: 8px; transform: translateY(-50%); }
.wk-col {
  position: relative;
  border-left: 1px solid var(--line);
  background-image: linear-gradient(to bottom, var(--line) 1px, transparent 1px);
  background-size: 100% calc(100% / var(--hours));
}

.event {
  position: absolute;
  overflow: hidden;
  display: flex; flex-direction: column; align-items: flex-start;
  padding: 4px 8px;
  border-radius: 8px;
  border-left: 5px solid var(--c);
  background: color-mix(in srgb, var(--c) var(--ev-mix), var(--surface));
  text-align: left;
  font-size: .9rem;
  line-height: 1.2;
  z-index: 1;
}
.event .ev-title { font-weight: 650; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.event .ev-time, .event .ev-loc { color: var(--muted); font-size: .88em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
/* Les lignes ne se tassent pas : si ça ne tient pas, fitText (app.js) le voit et réduit le texte */
.event:not(.short) > span { flex-shrink: 0; }
.event.short { flex-direction: row; gap: 6px; align-items: center; white-space: nowrap; }
.event.short .ev-title { white-space: nowrap; flex-shrink: 0; max-width: 100%; }
.event.short .ev-loc { display: none; }
/* Ajustements automatiques quand le texte ne tient pas (voir fitText dans app.js) */
.event.tight { padding: 2px 5px !important; border-left-width: 4px !important; }
.event.clip .ev-title { white-space: nowrap; text-overflow: ellipsis; }
.event.short .ev-time { flex-shrink: 1; min-width: 0; }

.now-line { position: absolute; left: 0; right: 0; height: 2px; background: var(--now); z-index: 2; pointer-events: none; }
.now-line::before {
  content: ""; position: absolute; left: -5px; top: -4px;
  width: 10px; height: 10px; border-radius: 50%; background: var(--now);
}

/* ---------- Mois ---------- */

.month { display: flex; flex-direction: column; height: 100%; }
.mo-head { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); border-bottom: 1px solid var(--line); }
.mo-wd { padding: 10px; text-align: center; color: var(--muted); font-weight: 600; }
.mo-grid {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  grid-template-rows: repeat(var(--weeks), minmax(0, 1fr));
}
.mo-cell {
  display: flex; flex-direction: column;
  min-height: 0; min-width: 0;
  padding: 4px 6px 6px;
  border-left: 1px solid var(--line);
  border-top: 1px solid var(--line);
}
.mo-cell:nth-child(7n + 1) { border-left: 0; }
.mo-cell:nth-child(-n + 7) { border-top: 0; }
.mo-cell.other { background: var(--surface-2); }
.mo-cell.other .mo-num { color: var(--muted); opacity: .6; }
.mo-cell.today { background: var(--today-bg); }
.mo-num {
  align-self: flex-start;
  min-width: 2em; height: 2em; border-radius: 50%;
  font-weight: 700; font-size: 1.05rem;
}
.mo-cell.today .mo-num { background: var(--accent); color: #fff; }
.mo-list { flex: 1; min-height: 0; overflow: hidden; display: flex; flex-direction: column; gap: 2px; }
.mo-ev {
  display: flex; align-items: center; gap: 6px;
  width: 100%;
  padding: 2px 4px;
  border-radius: 5px;
  font-size: .85rem;
  text-align: left;
  white-space: nowrap;
  flex-shrink: 0;
}
.mo-ev .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--c); flex-shrink: 0; }
.mo-ev .mo-time { color: var(--muted); font-variant-numeric: tabular-nums; }
.mo-ev .mo-title { overflow: hidden; text-overflow: ellipsis; font-weight: 550; }
.mo-ev.allday { background: var(--c); color: #fff; font-weight: 600; }
.mo-cell { position: relative; }
.mo-more {
  position: absolute; top: 6px; right: 6px;
  padding: 2px 9px; border-radius: 999px;
  background: var(--line-strong); color: var(--text);
  font-size: .85rem; font-weight: 700;
}

/* ---------- Fenêtre de détail ---------- */

.event-dialog {
  width: min(520px, calc(100vw - 32px));
  border: 0;
  border-radius: 20px;
  padding: 28px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgb(0 0 0 / .3);
}
.event-dialog::backdrop { background: rgb(15 23 42 / .45); }
.ed-person {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--c);
  color: #fff;
  font-weight: 600; font-size: .85rem;
}
.event-dialog h2 { margin: 14px 0 8px; font-size: 1.6rem; line-height: 1.2; }
.ed-when { font-weight: 600; margin: 0 0 8px; }
.ed-where { margin: 0 0 8px; color: var(--muted); }
.ed-where:empty, .ed-desc:empty { display: none; }
.ed-desc { white-space: pre-wrap; color: var(--muted); max-height: 30vh; overflow: auto; margin: 0; user-select: text; }
.ed-close {
  display: block;
  margin: 20px 0 0 auto;
  padding: 12px 24px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

/* ---------- Écrans étroits / portrait ---------- */

@container app (max-width: 1100px) {
  .topbar { grid-template-columns: 1fr auto; }
  .nav { grid-column: 1 / -1; grid-row: 2; justify-content: flex-start; }
  .clock-time { font-size: 2.4rem; }
}
@container app (max-width: 700px) {
  .topbar { padding-left: 16px; padding-right: 16px; gap: 12px; }
  .calendar { margin-left: 8px; margin-right: 8px; }
  .wk-row { grid-template-columns: 36px repeat(7, minmax(0, 1fr)); }
  .wk-day-head { flex-direction: column; align-items: center; gap: 0; }
  .event { padding: 2px 4px; font-size: .75rem; }
  .event .ev-time, .event .ev-loc { display: none; }
  .mo-wd { font-size: .75rem; padding: 6px 2px; }
  .mo-ev .mo-time { display: none; }
  .status { display: none; }
}

/* ---------- Rebond au toucher d'un rendez-vous ---------- */

.event.bounce, .chip.bounce, .mo-ev.bounce {
  animation: ev-bounce .45s cubic-bezier(.28, .84, .42, 1);
  z-index: 20 !important;
}
@keyframes ev-bounce {
  0%   { transform: scale(1, 1); }
  25%  { transform: scale(1.08, .88); }
  50%  { transform: scale(.94, 1.08) translateY(-4px); }
  72%  { transform: scale(1.03, .97); }
  100% { transform: scale(1, 1); }
}

/* La fenêtre de détail arrive en rebondissant, l'ornement (chat, lotus) avec un léger décalage */
.event-dialog[open] { animation: dialog-pop .5s cubic-bezier(.34, 1.56, .64, 1); }
.event-dialog[open] .ed-deco { animation: deco-hop .7s cubic-bezier(.34, 1.56, .64, 1) .12s both; }
@keyframes dialog-pop {
  from { transform: scale(.6) translateY(30px); opacity: 0; }
  60%  { opacity: 1; }
  to   { transform: scale(1) translateY(0); opacity: 1; }
}
@keyframes deco-hop {
  0%   { transform: translateY(26px) scale(.4) rotate(-12deg); opacity: 0; }
  55%  { transform: translateY(-10px) scale(1.08) rotate(6deg); opacity: 1; }
  80%  { transform: translateY(3px) scale(.97) rotate(-2deg); }
  100% { transform: translateY(0) scale(1) rotate(0); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .event.bounce, .chip.bounce, .mo-ev.bounce, .event-dialog[open], .event-dialog[open] .ed-deco { animation: none; }
}

/* ---------- Orientation forcée (voir applyOrientation dans app.js) ---------- */
/* Quand Android refuse de pivoter l'écran, c'est l'app elle-même qui pivote. */

body { container: app / inline-size; }

html[data-rotate="90"] body,
html[data-rotate="270"] body {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vh;
  height: 100vw;
  transform-origin: top left;
}
html[data-rotate="90"] body  { transform: rotate(90deg) translateY(-100%); }
html[data-rotate="270"] body { transform: rotate(-90deg) translateX(-100%); }
html[data-rotate="180"] body { transform: rotate(180deg); }
/* La fenêtre de détail s'affiche au-dessus de tout : on la pivote à part */
html[data-rotate="90"] .event-dialog  { rotate: 90deg; }
html[data-rotate="270"] .event-dialog { rotate: -90deg; }
html[data-rotate="180"] .event-dialog { rotate: 180deg; }

/* Menu du bouton d'orientation (la barre du haut passe devant le planning) */
.topbar { position: relative; z-index: 5; }
.orient { position: relative; }
.orient-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  min-width: 200px;
  padding: 6px;
  border-radius: 16px;
  background: var(--surface);
  background-color: color-mix(in srgb, var(--bg) 40%, white);
  border: 1px solid var(--line-strong);
  box-shadow: 0 12px 30px rgb(0 0 0 / .18);
}
.orient-menu button {
  text-align: left;
  padding: 11px 14px;
  border-radius: 10px;
  font-weight: 700;
  color: var(--text);
}
.orient-menu button:active { transform: scale(.98); }
.orient-menu button[aria-checked="true"] { background: var(--accent); color: #fff; }
.orient-menu button[aria-checked="true"]::after { content: " ✓"; }
